/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Chat;

import Core.Value;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author Mr Bao
 */
public class Util {

    // <editor-fold defaultstate="collapsed" desc="Khai bao">
    public static int keypressTimeout = 500;
    public static char keyCodesLowCaseVi[][] = {{' ', '0'}, {'.', '_', '@', ',', ':', '1', '?', '!', '$', '%', '&', '\'', '\"', '-', '(', ')', '/'}, {'a', 'ă', 'â', 'b', 'c', '2'}, {'d', 'đ', 'e', 'ê', 'f', '3'}, {'g', 'h', 'i', '4'}, {'j', 'k', 'l', '5'}, {'m', 'n', 'o', 'ô', 'ơ', '6'}, {'p', 'q', 'r', 's', '7'}, {'t', 'u', 'ư', 'v', '8'}, {'w', 'x', 'y', 'z', '9'}};
    public static char keyCodesLowCase[][] = {{' ', '0'}, {'.', '_', '@', ',', ':', '1', '?', '!', '$', '%', '&', '\'', '\"', '-', '(', ')', '/'}, {'a', 'b', 'c', '2'}, {'d', 'e', 'f', '3'}, {'g', 'h', 'i', '4'}, {'j', 'k', 'l', '5'}, {'m', 'n', 'o', '6'}, {'p', 'q', 'r', 's', '7'}, {'t', 'u', 'v', '8'}, {'w', 'x', 'y', 'z', '9'}};
    public static char keyCodesUpCase[][] = {{' ', '0'}, {'.', '_', '@', ',', ':', '1', '?', '!', '$', '%', '&', '\'', '\"', '-', '(', ')', '/'}, {'A', 'B', 'C', '2'}, {'D', 'E', 'F', '3'}, {'G', 'H', 'I', '4'}, {'J', 'K', 'L', '5'}, {'M', 'N', 'O', '6'}, {'P', 'Q', 'R', 'S', '7'}, {'T', 'U', 'V', '8'}, {'X', 'Y', 'Z', 'W', '9'}};
    public static char keyCodesUpCaseVi[][] = {{' ', '0'}, {'.', '_', '@', ',', ':', '1', '?', '!', '\'', '\"', '-', '(', ')', '/'}, {'A', 'Ă', 'Â', 'B', 'C', '2'}, {'D', 'Đ', 'E', 'Ê', 'F', '3'}, {'G', 'H', 'I', '4'}, {'J', 'K', 'L', '5'}, {'M', 'N', 'O', 'Ô', 'Ơ', '6'}, {'P', 'Q', 'R', 'S', '7'}, {'T', 'U', 'Ư', 'V', '8'}, {'X', 'Y', 'Z', 'W', '9'}};
    public static char keyCodesNumber[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    public static Font FontSmall = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    public static Font fontMedium = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
    public static Font fontChat = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    public static Font fontLinkWeb = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_ITALIC, Font.SIZE_SMALL);
    public static int color1 = makeColorAsInt(0, 225, 225, 225);
    public static int color2 = makeColorAsInt(0, 58, 90, 255);
    public static int color3 = makeColorAsInt(0, 0, 0, 220);
    public static int color21 = makeColorAsInt(0, 72, 118, 255);
    public static int color11 = makeColorAsInt(0, 255, 255, 255);
    public static int textColor = makeColorAsInt(0, 40, 40, 40);
    public static int colorBlue = makeColorAsInt(0, 58, 90, 255);
    public static int colorGreen = makeColorAsInt(0, 145, 44, 238);
    public static int colorPink = makeColorAsInt(0, 255, 105, 180);
    public static int colorBlue1 = makeColorAsInt(0, 72, 118, 255);
    public static int colorGreen1 = makeColorAsInt(0, 125, 34, 218);
    public static int colorPink1 = makeColorAsInt(0, 240, 128, 128);
    public static int colorBlue3 = makeColorAsInt(0, 0, 0, 220);
    public static int colorGreen3 = makeColorAsInt(0, 125, 38, 205);
    public static int colorPink3 = makeColorAsInt(0, 205, 96, 144);
    public static int[] colors = {colorBlue, colorGreen, colorPink};
    public static int[] colors1 = {colorBlue1, colorGreen1, colorPink1};
    public static int[] colors3 = {colorBlue3, colorGreen3, colorPink3};
    public static final int VERTICAL = 0;
    public static final int HORIZONTAL = 1;// </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Draw">
    public static void ClearScreen(Graphics g, int width, int height) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, width, height);
    }

    public static void GradientBox(Graphics g, int color1, int color2, int left, int top, int width, int height, int orientation) {
        int max = orientation == VERTICAL ? height : width;

        for (int i = 0; i < max; i++) {
            int color = midColor(color1, color2, max * (max - 1 - i) / (max - 1), max);

            g.setColor(color);

            if (orientation == VERTICAL) {
                g.drawLine(left, top + i, left + width - 1, top + i);
            } else {
                g.drawLine(left + i, top, left + i, top + height - 1);
            }
        }
    }

    public static void DrawTitleBar(Graphics g, String left, String center, String right, int width, int height) {
        GradientBox(g, color1, color2, 0, 0, width, fontMedium.getHeight() / 2, 0);
        g.setColor(color3);
        g.fillRect(0, fontMedium.getHeight() / 2, width, fontMedium.getHeight() / 2);
        g.setColor(210, 210, 210);

        if (left != null) {
            g.setFont(FontSmall);
            g.drawString(left, width / 40 + 6, FontSmall.getHeight() / 8, 0);
        }

        if (center != null) {
            g.setFont(fontMedium);
            g.drawString(center, width * 39 / 80 - fontMedium.stringWidth(center) / 2, -2, 0);
        }

        if (right != null) {
            g.setFont(FontSmall);
            g.drawString(right, width * 39 / 40 - FontSmall.stringWidth(right), FontSmall.getHeight() / 8, 0);
        }
    }

    public static void DrawBottomBar(Graphics g, String left, String center, String right, int width, int height) {
        GradientBox(g, color1, color2, 0, height - fontMedium.getHeight(), width, fontMedium.getHeight(), 0);
        g.setColor(color3);
        g.fillRect(0, height - fontMedium.getHeight() / 2, width, fontMedium.getHeight() / 2);
        g.setColor(210, 210, 210);
        g.setFont(FontSmall);

        if (left != null) {
            g.drawString(left, width / 40, height - FontSmall.getHeight() * 9 / 8, 0);
        }

        if (center != null) {
            g.drawString(center, width * 39 / 80 - FontSmall.stringWidth(center) / 2, height - FontSmall.getHeight() * 9 / 8, 0);
        }

        if (right != null) {
            g.drawString(right, width * 39 / 40 - FontSmall.stringWidth(right), height - FontSmall.getHeight() * 9 / 8, 0);
        }
    }

    static int midColor(int color1, int color2, int prop, int max) {
        int red =
                (((color1 >> 16) & 0xff) * prop + ((color2 >> 16) & 0xff) * (max - prop)) / max;

        int green =
                (((color1 >> 8) & 0xff) * prop + ((color2 >> 8) & 0xff) * (max - prop)) / max;

        int blue =
                (((color1 >> 0) & 0xff) * prop + ((color2 >> 0) & 0xff) * (max - prop)) / max;

        int color = red << 16 | green << 8 | blue;

        return color;
    }

    public static int makeColorAsInt(int alpha, int red, int green, int blue) {
        return alpha << 24 | red << 16 | green << 8 | blue;
    }// </editor-fold>

    public static String DelKey(String str, int index) {
        if (index == 0) {
            if (str.length() > 0) {
                return str.substring(1);
            } else {
                return "";
            }
        }
        String s = "";
        s += str.substring(0, index - 1);
        s += str.substring(index);
        return s;
    }

    public static boolean isNumericKeyPad(Canvas canvas) {
        try {
//            for (int i = 65; i < (65 + 26); i++) {
//                canvas.getKeyName(i);
//            }
            if ((canvas.getGameAction(48) == 0) && (canvas.getGameAction(49) == 0) && (canvas.getGameAction(50) == 0) && (canvas.getGameAction(51) == 0) && (canvas.getGameAction(52) == 0) && (canvas.getGameAction(53) == 0) && (canvas.getGameAction(54) == 0) && (canvas.getGameAction(55) == 0) && (canvas.getGameAction(56) == 0) && (canvas.getGameAction(57) == 0)) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return true;
        }
    }

    public static String SelectKey(String value, int pointer, int keyCode) {
        String str = "";
        str += value.substring(0, pointer);
        str += (char) keyCode;
        str += value.substring(pointer);
        return str;
    }

    public static String SelectNum(String value, int pointer, int keyCode) {
        String str = "";
        str += value.substring(0, pointer);
        str += keyCodesNumber[keyCode - Canvas.KEY_NUM0];
        str += value.substring(pointer);
        return str;
    }

    public static Value SelectKey(Value value, int keycode, char[][] keywords) {
        if (keycode < Canvas.KEY_NUM0) {
            value.timer = System.currentTimeMillis();
            return null;
        }
        if (value.lastKey == keycode) {
            if (System.currentTimeMillis() - value.timer > keypressTimeout) {
                value.timer = System.currentTimeMillis();
                value.index = 0;
                String str = "";
                str += value.string.substring(0, value.pointer);
                str += keywords[keycode - Canvas.KEY_NUM0][value.index];
                str += value.string.substring(value.pointer);
                value.string = str;
                if (value.password != null) {
                    value.password = ResetPassword(value.password);
                    str = "";
                    str += value.password.substring(0, value.pointer);
                    str += keywords[keycode - Canvas.KEY_NUM0][value.index];
                    str += value.password.substring(value.pointer);
                    value.password = str;
                }
                value.timer = System.currentTimeMillis();
                value.lastKey = keycode;
                value.pointer++;
                return value;
            } else {
                value.index++;
                if (value.index == keywords[keycode - Canvas.KEY_NUM0].length) {
                    value.index = 0;
                }
                if (value.string.length() > 0) {
                    value.string = DelKey(value.string, value.pointer);
                    if (value.password != null) {
                        value.password = DelKey(value.password, value.pointer);
                    }
                    if (value.pointer > 0) {
                        value.pointer--;
                    }
                }
                String str = "";
                str += value.string.substring(0, value.pointer);
                str += keywords[keycode - Canvas.KEY_NUM0][value.index];
                str += value.string.substring(value.pointer);
                value.string = str;
                if (value.password != null) {
                    str = "";
                    str += value.password.substring(0, value.pointer);
                    str += keywords[keycode - Canvas.KEY_NUM0][value.index];
                    str += value.password.substring(value.pointer);
                    value.password = str;
                }
                value.timer = System.currentTimeMillis();
                value.lastKey = keycode;
                value.pointer++;
                return value;
            }
        } else {
            value.index = 0;
            String str = "";
            str += value.string.substring(0, value.pointer);
            str += keywords[keycode - Canvas.KEY_NUM0][value.index];
            str += value.string.substring(value.pointer);
            value.string = str;
            if (value.password != null) {
                value.password = ResetPassword(value.password);
                str = "";
                str += value.password.substring(0, value.pointer);
                str += keywords[keycode - Canvas.KEY_NUM0][value.index];
                str += value.password.substring(value.pointer);
                value.password = str;
            }
            value.timer = System.currentTimeMillis();
            value.lastKey = keycode;
            value.pointer++;
            return value;
        }
    }

    public static String ResetPassword(String password) {
        String str = "";
        for (int i = 0; i < password.length(); i++) {
            str += "*";
        }
        return str;
    }
}
